package com.luck.emp.exp.logic;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.criterion.Order;

import com.luck.emp.entity.EmpTExpress;
import com.luck.emp.exp.qry.ExpQry;
import com.luck.pf.common.entity.UserBean;
import com.luck.pf.framework.dao.BaseJdbcDao;
import com.luck.pf.framework.logic.BaseLogic;
import com.luck.pf.util.CriteriaUtil;
import com.luck.pf.util.DateUtil;
import com.luck.pf.util.Page;

/**
 * 说明 Copyright: Copyright (c) 2011 深圳**科技
 * 
 * @author
 * 
 */
public class ExpLogic extends BaseLogic implements IExpLogic {
	private BaseJdbcDao jdbcDao;

	/**
	 * 查找分页信息
	 */
	public Page findExpPage(ExpQry qry) {
		Criteria criteria = baseDao.createCriteria(EmpTExpress.class);
		CriteriaUtil.addFullLike(criteria, "expressCode", qry.getExpressCode());
		CriteriaUtil.addFullLike(criteria, "expressName", qry.getExpressName());
		Order order = Order.desc("expressCode");
		criteria.addOrder(order);
		criteria.createAlias("dept", "d");
		filterData(criteria, qry.getUserInfo(), "d", null);
		return baseDao.pagedQuery(criteria, qry.getPageNo(), qry.getPageSize());
	}

	/**
	 * 保存
	 **/
	public String saveExp(UserBean userBean, EmpTExpress entity)
			throws Exception {
		StringBuffer bf = new StringBuffer();
		// 检查保存逻辑
		if (entity.getId() == null) {
			entity.setCreateUser(userBean.getUser());
			entity.setCreateDate(DateUtil.getDate());
			entity.setDeptId(userBean.getDeptId());
		}
		// 如果保存逻辑不通过则会在BF中增加提示信息，如不为空，则抛出错误提示。
		if (StringUtils.isBlank(bf.toString())) {
			super.save(entity);
		} else
			throw new Exception(bf.toString());
		return bf.toString();
	}

	/**
	 * 删除
	 */
	public String deleteExp(UserBean userBean, EmpTExpress entity)
			throws Exception {
		StringBuffer bf = new StringBuffer();
		// 检查删除逻辑
		int count = super.baseDao.count(
				"  select count(*) from EmpTExpInfo t where id = ?",
				entity.getId());
		if (count > 0)
			bf.append("<br>此承运商已经有业务数据");
		entity = super.get(EmpTExpress.class, entity.getId());
		if(!entity.getDeptId().equals(userBean.getDeptId()))
			bf.append("<br>不属于您所在的机构");
		// 如果删除逻辑不通过则会在BF中增加提示信息，如不为空，则抛出错误提示。
		if (StringUtils.isBlank(bf.toString())) {
			super.baseDao.delete(entity);
		} else
			throw new Exception(bf.toString());
		return bf.toString();
	}

	public BaseJdbcDao getJdbcDao() {
		return jdbcDao;
	}

	public void setJdbcDao(BaseJdbcDao jdbcDao) {
		this.jdbcDao = jdbcDao;
	}

}